<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from "@/components/Charts/mixins/resize"
import {get} from "@/api/requestCommon";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      chartData:[],
      oweList:[]
    }
  },

  mounted() {
    this.countAmount()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    countAmount(){
      get('receipts/countAmount').then(res=>{
        if(res.code===200){
          this.chartData=res.data==null?[]:res.data
          this.$nextTick(() => {
            this.initChart()
          })
        }
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions() {
      let chartData=this.chartData;
      let x=[];
      let y=[];
      let oweY=[]
      chartData.forEach(chart=>{
        x.push(chart.collDate);
        y.push(chart.actual);
        oweY.push(chart.owe)
      })



      this.chart.setOption({
        title: {
          text: '近一周统计'
        },
        xAxis: {
          data: x,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['实收金额','欠费金额']
        },
        series: [
          {
          name: '实收金额', itemStyle: {
            normal: {
              color: '#38d7b9',
              lineStyle: {
                color: '#38d7b9',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: y,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
          {
            name: '欠费金额', itemStyle: {
              normal: {
                color: '#FF005A',
                lineStyle: {
                  color: '#FF005A',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: oweY,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          }]
      })
    }
  }
}
</script>
